#ifndef _frame_driver_H_
#define _frame_driver_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "stdint.h"

#include "stdio.h"
#define dFrame_LOG(...) printf(__VA_ARGS__) //日志输出

#define dIdentify_Invert(id) ((~(id)) & 0xFF)//标识符取反
#define dDataLength_MSB(len) (((len)>>8)&0xFF)//截取数据长度高8位
#define dDataLength_LSB(len) ((len)&0xFF)//截取数据长度低8位
#define dDataLength_Merge(MSB,LSB) (((((uint16_t )(MSB)) << 8)|(uint16_t )(LSB)) & 0xFFFF)//合并得到长帧的数据长度
#define dFrame_FormatSize(id) (((id) > 0x80)?4:3)//帧格式大小
#define dFrame_Length_Get(id,DataLength) ((uint16_t )dFrame_FormatSize(id) + DataLength)//获取帧长度:id-标识符，DataLength-用户数据长度

typedef struct
{
	uint8_t number;//帧种类数
	uint8_t pos;//帧标识集可用位置
	uint8_t *pbuf;//所有帧标识
}Frame_Identify_StructDef;//帧标识集

typedef struct
{
	uint32_t size; //帧缓冲区字节数
	uint8_t *pbuf;//缓冲区
	uint32_t pos;//位置点
}Frame_Buffer_StructDef;//缓冲区

typedef enum
{
	eFrame_State_ID = 0,//核对ID
	eFrame_State_Bytes,//核对帧字节数
	eFrame_State_Receive,//接收数据
}Frame_State_EnumDef;//帧接收状态

typedef struct
{
	Frame_State_EnumDef state;//帧接收状态
	uint8_t identify;//接收的帧标识
	uint32_t size;//用户数据字节数
	uint8_t *pbuf;//帧用户数据缓冲区;注:不含帧头、数据字节数
}Frame_Data_StructDef;

typedef struct
{
	//内部变量区域：请勿修改
	uint8_t state;//帧状态:0--不可用,!0--可用
	Frame_Identify_StructDef identify;//帧集合
	Frame_Buffer_StructDef rx;//接收缓冲区

	//共享变量区域
	Frame_Data_StructDef data_rx;//接收的数据
}frame_handler;

extern int frame_init(frame_handler *p, uint8_t number, uint32_t rx_size);
extern void frame_DeInit(frame_handler *p);
extern int frame_identify_add(frame_handler *p, uint8_t identify);
extern int frame_find(frame_handler *p, uint8_t data);
extern int frame_find_clear(frame_handler *p);
extern void frame_data_printf(Frame_Data_StructDef data);
extern uint8_t *frame_format_generate(uint8_t identify, uint32_t size, uint8_t *pbuf);
extern uint16_t frame_CRC16_ParityGet(uint8_t *data, uint16_t len);
extern uint8_t frame_ChecksumGet(uint8_t *data, uint16_t len);

#ifdef __cplusplus
}
#endif
#endif


